﻿using CloudStorageLight.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Http;
using CloudStorageLight.Core.Web;
using System.Web.OData.Extensions;
using System.Web.Http.OData.Extensions;
using CloudStorageLight.SampleWeb.Models;
using CloudStorageLight.SampleWeb.Controllers;

namespace CloudStorageLight.SampleWeb
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //TODO:CloudStorageLight
            //    <handlers>
            //      <add name="FileManagedDllExtensionStorage" path="f/*" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
            //    </handlers>
            routes.MapRoute(
                "files", "f/{*id}",
                 new
                 {
                     controller = "Home",    // コントローラ名
                     action = "Index",        // アクション名
                 }
            );

            var config = System.Web.Http.GlobalConfiguration.Configuration;

            var builder = DataController.GetModelBuilder("Data");
            var model = builder.GetEdmModel();

            var odataRouteV4 = config.MapODataServiceRoute(
                routeName: "odataV4",
                routePrefix: "odata",
                model: model);

            //var constraint = new ODataVersionRouteConstraint(new { V = "4" });
            //odataRouteV4.Constraints.Add("VersionConstraintV4", constraint);


            var defaultConventions = System.Web.Http.OData.Routing.Conventions.ODataRoutingConventions.CreateDefault();
            var conventions = new List<System.Web.Http.OData.Routing.Conventions.IODataRoutingConvention>();
            foreach (var convention in defaultConventions)
            {
                if (convention is System.Web.Http.OData.Routing.Conventions.MetadataRoutingConvention)
                {
                    // Don't need to special case these conventions.
                    conventions.Add(convention);
                }
                else
                {
                    conventions.Add(new VersionedRoutingConvention(convention, "V3"));
                }
            }

            var builder3 = DataV3Controller.GetModelBuilder("Data");
            var odataRoute = config.Routes.MapODataServiceRoute("odataV3", "odatav3", builder3.GetEdmModel(),
                new System.Web.Http.OData.Routing.DefaultODataPathHandler(), conventions);


            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}
